home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Aminet / dev / c / DosList.lha / DosList.c < prev   
C/C++ Source or Header  |  1996-10-17  |  5KB  |  178 lines

  1. /***********************************************************************************/
  2. /*                                                                                 */
  3. /* This is meant to be just a didactic example for who want to know to get device, */
  4. /* volume or assign names, but it can be used for any thing you want.              */
  5. /*                                                                                 */
  6. /* No limitations. Really!                                                         */
  7. /*                                                                                 */
  8. /* Author: Michele Locati                                                          */
  9. /* EMail : mlocati@rocketmail.com                                                  */
  10. /* Date  : 17/10/97 (friday!)                                                      */
  11. /*                                                                                 */
  12. /***********************************************************************************/
  13. /***********************************************************************************/
  14. /*                                                                                 */
  15. /* Compiled with Dice v3.20 (1.6.96) for Amigas with 2.0+                          */
  16. /*                                                                                 */
  17. /***********************************************************************************/
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <clib/dos_protos.h>
  22. #include <dos/dosextens.h>
  23.  
  24. BOOL GetDosList(void);
  25. void FreeDosList(void);
  26. STRPTR BCPL2STRPTR(BSTR s);
  27.  
  28. typedef struct
  29. {
  30.     STRPTR    Name;
  31.     STRPTR    Drive;
  32. } Volume;
  33.  
  34. typedef struct
  35. {
  36.     STRPTR    Name;
  37.     STRPTR    Path;
  38. } Assign;
  39.  
  40. Volume *Volumes;
  41. Assign *Assigns;
  42. STRPTR *Devices;
  43. int NumVolumes, NumAssigns, NumDevices;
  44. char strtmp[256];
  45.  
  46. /* wbmain() is called when starting from Workbench*/
  47. int wbmain(void)
  48. {
  49.     return(main());
  50. }
  51.  
  52. int main(void)
  53. {
  54.     int i;
  55.     
  56.     GetDosList();
  57.  
  58.     printf("Assigns: %d\n", NumAssigns);
  59.     for (i=0; i<NumAssigns; i++)
  60.         printf("\t%s:\t%s\n", Assigns[i].Name, Assigns[i].Path);
  61.     
  62.     printf("\nVolumes: %d\n", NumVolumes);
  63.     for (i=0; i<NumVolumes; i++)
  64.         printf("\t%s:\t%s\n", Volumes[i].Drive, Volumes[i].Name);
  65.  
  66.     printf("\nDevices: %d\n", NumDevices);
  67.     for (i=0; i<NumDevices; i++)
  68.         printf("\t%s\n", Devices[i]);
  69.  
  70.     FreeDosList();
  71.  
  72.     return(0);
  73. }
  74.  
  75. BOOL GetDosList(void)
  76. {
  77.     struct DosList *dl;
  78.     int i;
  79.  
  80.     
  81.     printf("Getting assigns...\n");
  82.     NumAssigns=0;
  83.     dl=LockDosList(LDF_ASSIGNS | LDF_READ);
  84.     while (dl=NextDosEntry(dl, LDF_ASSIGNS))
  85.         NumAssigns++;
  86.     UnLockDosList(LDF_ASSIGNS | LDF_READ);
  87.     Assigns=(Assign *)malloc(NumAssigns * sizeof(Assign));
  88.     i=0;
  89.     dl=LockDosList(LDF_ASSIGNS | LDF_READ);
  90.     while (dl=NextDosEntry(dl, LDF_ASSIGNS))
  91.     {
  92.         Assigns[i].Name=BCPL2STRPTR(dl->dol_Name);
  93.         NameFromLock(dl->dol_Lock, strtmp, 255);
  94.         Assigns[i].Path=strdup(strtmp);
  95.         i++;
  96.     }
  97.     UnLockDosList(LDF_ASSIGNS | LDF_READ);
  98.  
  99.  
  100.     printf("Getting volumes...\n");
  101.     NumVolumes=0;
  102.     dl=LockDosList(LDF_VOLUMES | LDF_READ);
  103.     while (dl=NextDosEntry(dl, LDF_VOLUMES))
  104.         NumVolumes++;
  105.     UnLockDosList(LDF_VOLUMES | LDF_READ);
  106.     Volumes=(Volume *)malloc(NumVolumes * sizeof(Volume));
  107.     i=0;
  108.     dl=LockDosList(LDF_VOLUMES | LDF_READ);
  109.     while (dl=NextDosEntry(dl, LDF_VOLUMES))
  110.     {
  111.         Volumes[i].Drive=strdup(((struct Task *)dl->dol_Task->mp_SigTask)->tc_Node.ln_Name);
  112.         Volumes[i++].Name=BCPL2STRPTR(dl->dol_Name);
  113.     }
  114.     UnLockDosList(LDF_VOLUMES | LDF_READ);
  115.  
  116.  
  117.     printf("Getting devices...\n");
  118.     NumDevices=0;
  119.     dl=LockDosList(LDF_DEVICES | LDF_READ);
  120.     while (dl=NextDosEntry(dl, LDF_DEVICES))
  121.         NumDevices++;
  122.     UnLockDosList(LDF_DEVICES | LDF_READ);
  123.     Devices=(STRPTR *)malloc(NumDevices * sizeof(STRPTR));
  124.     i=0;
  125.     dl=LockDosList(LDF_DEVICES | LDF_READ);
  126.     while (dl=NextDosEntry(dl, LDF_DEVICES))
  127.         Devices[i++]=BCPL2STRPTR(dl->dol_Name);
  128.     UnLockDosList(LDF_DEVICES | LDF_READ);
  129. }
  130.  
  131. STRPTR BCPL2STRPTR(BSTR s)
  132. {
  133.     STRPTR ris;
  134.     STRPTR eq;
  135.     
  136.     if (!s)
  137.         return(NULL);
  138.     eq=(STRPTR)BADDR(s);
  139.     if (!eq[0])
  140.         return(NULL);
  141.     ris=(STRPTR)malloc(eq[0] + 1);
  142.     memcpy(ris, eq + 1, eq[0]);
  143.     ris[eq[0]]=0;
  144.     return(ris);
  145. }
  146.  
  147. void FreeDosList(void)
  148. {
  149.     int i;
  150.  
  151.     
  152.     printf("Freeing assigns...");
  153.     for (i=0; i<NumAssigns; i++)
  154.     {
  155.         free(Assigns[i].Name);
  156.         free(Assigns[i].Path);
  157.     }
  158.     free(Assigns);
  159.     NumAssigns=0;
  160.  
  161.  
  162.     printf("\nFreeing volumes...\n");
  163.     for (i=0; i<NumVolumes; i++)
  164.     {
  165.         free(Volumes[i].Drive);
  166.         free(Volumes[i].Name);
  167.     }
  168.     free(Volumes);
  169.     NumVolumes=0;
  170.  
  171.     
  172.     printf("Freeing devices...\n");
  173.     for (i=0; i<NumDevices; i++)
  174.         free(Devices[i]);
  175.     free(Devices);
  176.     NumDevices=0;
  177. }
  178.